package com.healthybear.demo.languagechange

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2

/**
 * Implementation2Activity demonstrates language switching using Configuration.updateConfiguration()
 * This is the legacy approach with manual activity restart
 * 
 * Requirements addressed:
 * - 5.1: Display ViewPager2 with multiple pages
 * - 5.2: Display RecyclerView with list items
 * - 5.3: Embed language switching fragment
 * - 5.4: Update all content when language is changed
 * - 5.5: Maintain selected language when navigating between ViewPager2 pages
 */
class Implementation2Activity : BaseActivity() {
    
    private lateinit var viewPager: ViewPager2
    private lateinit var recyclerView: RecyclerView
    private lateinit var recyclerAdapter: RecyclerAdapter
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_implementation)
        
        setupViewPager()
        setupRecyclerView()
        setupLanguageFragment()
    }
    
    /**
     * Requirement 5.1: Set up ViewPager2 with FragmentStateAdapter
     */
    private fun setupViewPager() {
        viewPager = findViewById(R.id.viewPager)
        val adapter = ViewPagerAdapter(this)
        viewPager.adapter = adapter
    }
    
    /**
     * Requirement 5.2: Configure RecyclerView with sample data adapter
     */
    private fun setupRecyclerView() {
        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        
        // Create sample data for testing language switching
        val sampleItems = RecyclerAdapter.createSampleData()
        
        recyclerAdapter = RecyclerAdapter(this, sampleItems)
        recyclerView.adapter = recyclerAdapter
    }
    
    /**
     * Requirement 5.3: Embed LanguageFragment with Method 2 configuration
     */
    private fun setupLanguageFragment() {
        val languageFragment = LanguageFragment.newInstance(LanguageUtils.ImplementationType.CONFIGURATION_UPDATE)
        
        supportFragmentManager.beginTransaction()
            .replace(R.id.languageFragmentContainer, languageFragment)
            .commit()
    }
    
    override fun getImplementationType(): LanguageUtils.ImplementationType {
        return LanguageUtils.ImplementationType.CONFIGURATION_UPDATE
    }
    
    /**
     * Refresh RecyclerView content when language changes
     * Requirement 5.5: Update all content when language is changed
     */
    fun refreshRecyclerViewContent() {
        if (::recyclerAdapter.isInitialized) {
            recyclerAdapter.refreshLanguage()
        }
    }
    
    /**
     * ViewPager2 adapter for managing content fragments
     * Requirement 5.1, 5.5: Handle ViewPager2 with multiple pages and maintain language
     */
    private class ViewPagerAdapter(activity: Implementation2Activity) : FragmentStateAdapter(activity) {
        
        override fun getItemCount(): Int = 3
        
        override fun createFragment(position: Int): Fragment {
            return ContentFragment.newInstance(position)
        }
    }
}